// index.ts
// 获取应用实例
import {TripService} from "../../service/trip";
import {ProfileService} from "../../service/profile";
import {CarService} from "../../service/car";
Page({
  socketTask: undefined as WechatMiniprogram.SocketTask | undefined,
  data: {
    setting :{
      skew: 0,
      rotate: 0,
      showLocation: true,
      showScale: false,
      subKey: '',
      layerStyle: 1,
      enableZoom: true,
      enableScroll: true,
      enableRotate: true,
      showCompass: false,
      enable3D: false,
      enableOverlooking: true,
      enableSatellite: false,
      enableTraffic: false,
    },
    latitude: 30.528310553395897 ,
    longitude: 104.1083129286194,
    is3D:true,
    isOverLooKing:true,
    scale:15,
    markers: [
      {
        id: 3,
        latitude: 30.528310553395897,
        longitude: 104.1083129286194,
        iconPath: '/image/car.png',
        customCallout: {
          anchorY: 10,
          anchorX: 0,
          display: 'ALWAYS',
        },
        width:40,
        height:20
      },
      {
        id: 1,
        latitude: 32.528310553395897,
        longitude: 104.1083129286194,
        iconPath: '/image/car.png',
        customCallout: {
          anchorY: 10,
          anchorX: 0,
          display: 'ALWAYS',
        },
        width:40,
        height:20
      }
    ],
    avatarUrl:"/image/account.png"
  },
  onMyLocationTap(){ // 获取我等位置
    this.socketTask?.close({})

    wx.getLocation({
      type:"gcj02",
      success:result => {
        this.setData({
          latitude: result.latitude,
          longitude: result.longitude
        })
      },
      fail:(res) => {
        console.log(res)
        wx.showToast({
          title: '获取位置失败,请前往设置页授权',
          icon: 'none',
          duration: 2000
        })
      }
    })
  },
  onShow() {

    const avatarUrl = wx.getStorageSync("avatarUrl")
    if (avatarUrl){
      console.log(avatarUrl)
      this.setData({
        avatarUrl: avatarUrl
      })
    }
  },
  onLoad() {
    this.socketTask = CarService.subscribe((msg: any)=>{
      console.log(msg)
    })


    this.socketTask.onError((res)=>{
      console.log(res)
    })
    this.socketTask.onOpen((res)=>{
      console.log(res)
    })

    // this.moveCars()
    // this.onMyLocationTap()
  },
  moveCars(){ // 移动小车
    const mapContext = wx.createMapContext("map"); // 获取地图
    const dest = {
      latitude: 32.528310553395897,
      longitude: 104.1083129286194,
    }

    const moveCar = () =>{
      dest.longitude += 0.1
      dest.latitude   += 0.1
      // 改变位置
      mapContext.translateMarker({
        destination:{
          latitude:dest.latitude ,
          longitude:dest.longitude
        },
        markerId:3, // markerId
        rotate:0,
        duration:10000,
        autoRotate:false,
        animationEnd:moveCar
      })
    }
    moveCar()
  },
  async onScanClicked(){
    const trips = await TripService.GetTrips(1)
    if (trips.length != undefined || trips?.trips?.length != undefined) {
      wx.showModal({
        title:"行程提醒",
        content:"当前有未完成的行程",
        success:()=>{
          wx.navigateTo({
            url:`/pages/driving/driving?trip_id=${trips.trips[0]?.id}`
          })
        }
      })

      return
    }

    wx.showToast({
      title:"请随便扫一个二维码",
      icon:"none",
      duration:3000
    })


   setTimeout(()=>{
     wx.scanCode({
       success:async (result)=>{
         const carId = '634a275e3acf7638cefaa066'
         const redirectURL = `/pages/lock/lock?car_id=${carId}`

         const prof = await ProfileService.getProfile()
         console.log(prof)
         console.log(result)
         if (prof.identity_Status === 2){
           wx.navigateTo({
             url:redirectURL
           })
         }else {
           wx.showModal({
             title:"身份认证",
             content:"需要身份认证才能租车",
             success:(res)=>{
               if (res.confirm) {
                 wx.showLoading({
                   title:"扫描中，请稍后"
                 })
                 wx.hideLoading()

                 wx.navigateTo({
                   "url": `/pages/register/register?redirect=${encodeURIComponent(redirectURL)}`
                 })
               } else if (res.cancel) {
                 console.log('用户点击取消')
               }
             }
           })
         }
       },
       fail:()=>{
         wx.showToast({
           title:"请扫租车车的二维码",
           icon:"none",
           duration:3000
         })
       },
       complete(){
         wx.hideLoading()
       }
     })
   },1000)
  },
  onMyTripsTap(){
    wx.navigateTo({
      url:"/pages/mytrips/mytrips"
    })
  }
})
